//
//  TableCell.m
//  quickBatchManager
//
//  Created by jack on 15/9/23.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "TableCell.h"
#import "EditProductViewController.h"
@implementation TableCell
@synthesize tables,contentView;
- (void)awakeFromNib {
    contentView.layer.borderWidth = 1;
    contentView.layer.borderColor = [UIColor grayColor].CGColor;
   // [tables setColumnsWidths:[NSArray arrayWithObjects:@120,@120 ,nil]];
    //[tables addRecord:[NSArray arrayWithObjects:@"颜色",@"码数", nil]];
    
    [_editBtn bk_whenTapped:^{
        EditProductViewController *vc=[[EditProductViewController alloc] init];
        vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        vc.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        _parentVC.definesPresentationContext = YES;
        [_parentVC presentViewController:vc animated:YES completion:nil];
    }];
    
    _deleteBtn.userInteractionEnabled = YES;
    [_deleteBtn bk_whenTapped:^{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否删除产品" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        [alertView show];
    }];

}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 初始化时加载collectionCell.xib文件
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
        
        // 如果路径不存在，return nil
        if (arrayOfViews.count < 1)
        {
            return nil;
        }
        // 如果xib中view不属于UICollectionViewCell类，return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
        {
            return nil;
        }
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
        self.contentView.layer.borderWidth = 1;
    }
    return self;
}

-(void)setData:(NSString *)modelNo
{

    LKDBHelper *helper  = [LKDBHelper getUsingLKDBHelper];
    NSMutableArray *productInfoSizeArray = [helper search:[ProductInfoModel class] where:[NSString stringWithFormat:@"productId = %@ group by sizeName",modelNo] orderBy:nil offset:0 count:0];
    int width = 240.0/([productInfoSizeArray count]+1)+1;
    NSMutableArray *colums = [NSMutableArray array];
    for (int i=0;i<[productInfoSizeArray count];i++) {
        [colums addObject:[NSString stringWithFormat:@"%d",width]];
    }
        [colums addObject:[NSString stringWithFormat:@"%d",width]];
        [tables setColumnsWidths:colums];
    
    NSMutableArray *titleColums = [NSMutableArray array];
    for (int i=0;i<[productInfoSizeArray count];i++) {
        ProductInfoModel *model = [productInfoSizeArray objectAtIndex:i];
        if(i==0)
        {
            [titleColums addObject:@"颜色"];
        }
            [titleColums addObject:model.sizeName];
    }
     [tables addRecord:titleColums];
    
//    NSMutableArray *productInfoColorArray = [helper search:[ProductInfoModel class] where:[NSString stringWithFormat:@"modelNo = %@ group by colorName",modelNo] orderBy:nil offset:0 count:0];
//    NSMutableArray *productInfoArray = [helper search:[ProductInfoModel class] where:[NSString stringWithFormat:@"modelNo = %@",modelNo] orderBy:nil offset:0 count:0];
//   
//    for (int i=0; i<productInfoColorArray.count; i++) {
//        NSMutableArray *numberColums = [NSMutableArray array];
//        for (int j=0; j<productInfoSizeArray.count; j++) {
//            ProductInfoModel *model = [productInfoArray objectAtIndex:i*j];
//         //   numberColums[i][j] = [NSString stringWithFormat:@"%d",model.productNumber];
//            [numberColums addObject:[NSString stringWithFormat:@"%d",model.productNumber]];
//        }
//        
//        ProductInfoModel *model = [productInfoColorArray objectAtIndex:i];
//        [numberColums insertObject:model.colorName atIndex:0];
//        [tables addRecord:numberColums];
//        
//    }
    
    
    NSMutableArray *productInfoColorArray = [helper search:[ProductInfoModel class] where:[NSString stringWithFormat:@"productId = %@ group by colorName",modelNo] orderBy:nil offset:0 count:0];
    NSMutableArray *productInfoArray = [helper search:[ProductInfoModel class] where:[NSString stringWithFormat:@"productId = %@",modelNo] orderBy:nil offset:0 count:0];
    
    for (int i=0; i<productInfoColorArray.count; i++) {
        NSMutableArray *numberColums = [NSMutableArray array];
        ProductInfoModel *colormodel = [productInfoColorArray objectAtIndex:i];
        [numberColums addObject:colormodel.colorName];
        for (int j=0; j<productInfoSizeArray.count; j++) {
            ProductInfoModel *sizemodel = [productInfoSizeArray objectAtIndex:j];
            int number = 0;
            for (int z=0; z<productInfoArray.count; z++) {
                ProductInfoModel *model = [productInfoArray objectAtIndex:z];
                if([model.colorName isEqualToString:colormodel.colorName] && [model.sizeName isEqualToString:sizemodel.sizeName])
                {
                    number+=model.productNumber;
                }
            }
            [numberColums addObject:[NSString stringWithFormat:@"%d",number]];
            
        }
        
        
        [tables addRecord:numberColums];
        
    }
    
    
}

@end
